package com.zhang.learn.io.basic;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.*;

/**
 * @author zhanghaolian
 * @date 2022/7/28 23:40
 */
@Slf4j
public class FileOps {
    /**
     * 测试文件拷贝，一个字节一个字节拷贝
     */
    @Test
    public void testCopyFile() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File outputFile  = new File(FileOps.class.getResource("/").getFile() + "logging.properties1");
            if (outputFile.exists()) {
                log.info("文件：" + outputFile.getName() + "已存在，将已有的文件删除，以便重新拷贝");
                outputFile.delete();
            }
            outputFile.createNewFile();

            // 创建流
            inputStream = new FileInputStream(FileOps.class.getResource("/logging.properties").getFile());
            outputStream = new FileOutputStream(outputFile);

            // 2. 使用流
            int n = 0;
            while ((n = inputStream.read()) > 0) {
                outputStream.write(n);
            }
            // 3. 关闭流
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 文件拷贝，一次拷贝多个字节
     */
    @Test
    public void testCopyFile2() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File outputFile  = new File(FileOps.class.getResource("/").getFile() + "logging.properties1");
            if (outputFile.exists()) {
                log.info("文件：" + outputFile.getName() + "已存在，将已有的文件删除，以便重新拷贝");
                outputFile.delete();
            }
            outputFile.createNewFile();

            // 创建流
            inputStream = new FileInputStream(FileOps.class.getResource("/logging.properties").getFile());
            outputStream = new FileOutputStream(outputFile);

            // 2. 使用流
            byte [] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer,0,len);
            }
            // 3. 关闭流
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
